﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Web;




namespace SBLib.Common
{
    public static class CommonFunc
    {

        public static bool IsSsl()
        {
            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("https"))
                return true;
            else
                return false;
        }


        /// <summary>
        /// Randomly sort collection
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="ListCol">Collection</param>
        /// <returns>Sorted Collection</returns>
        public static List<T> RandomSortListCollection<T>(List<T> ListCol)
        {
            try
            {
              return ListCol.OrderBy(col => Guid.NewGuid()).ToList();
            }
            catch (Exception)
            {

                return null;
            }
        }

        /// <summary>
        /// Prepare object with out Null
        /// </summary>
        /// <param name="prmModel">Class object</param>
        /// <returns></returns>
        public static object PrepareObject(object prmModel)
        {
            try
            {
                var prmType = prmModel.GetType();
                int ctr = prmType.GetProperties().Count();

                foreach (PropertyInfo item in prmType.GetProperties())
                {
                    var localVal = item.GetValue(prmModel, null);

                    if (item.PropertyType == typeof(string) && item.CanWrite)
                    {
                        if (localVal == null)
                        {
                            localVal = "";
                        }
                        item.SetValue(prmModel, localVal, null);
                    }
                    if (item.PropertyType == typeof(char) && item.CanWrite)
                    {
                        if (localVal == null)
                        {
                            localVal = ' ';
                        }
                        item.SetValue(prmModel, localVal, null);
                    }
                    else if (item.PropertyType == typeof(bool) && item.CanWrite)
                    {
                        if (localVal == null)
                        {
                            localVal = false;
                        }
                        item.SetValue(prmModel, localVal, null);
                    }
                    else if (item.PropertyType == typeof(int) && item.CanWrite)
                    {
                        if (localVal == null)
                        {
                            localVal = 0;
                        }
                        item.SetValue(prmModel, localVal, null);
                    }
                    else if (item.PropertyType == typeof(long) && item.CanWrite)
                    {
                        if (localVal == null)
                        {
                            localVal = 0;
                        }
                        item.SetValue(prmModel, localVal, null);
                    }
                    else if (item.PropertyType == typeof(Nullable<DateTime>) && item.CanWrite)
                    {
                        if (localVal != null)
                        {
                            try
                            {
                                item.SetValue(prmModel, localVal, null);
                            }
                            catch (Exception)
                            {
                                item.SetValue(prmModel, null, null);
                            }
                        }
                        else
                        {
                            item.SetValue(prmModel, null, null);
                        }
                    }
                }

                return prmModel;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

 
        /// <summary>
        /// Get Customer GUID
        /// </summary>
        /// <param name="prmPrefix_Char">Prefix for the guid</param>
        /// <returns>Generated Guid</returns>
        public static string GetCustomGuid(string prmPrefix_Char)
        {
            try
            {
                return string.Format("{0}-{1}", prmPrefix_Char.Trim(), Guid.NewGuid().ToString().ToUpper());
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// Random password generation
        /// </summary>
        /// <param name="PasswordLength">Password length</param>
        /// <returns></returns>
        public static string GetRandomPassword(int PasswordLength)
        {
            try
            {
                string password = string.Empty;
                char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();

                Random random = new Random();
                
                for (int i = 0; i < PasswordLength; i++)
                {
                    int x = random.Next(1, chars.Length);
                
                    //Don't Allow Repetation of Characters            
                    if (!password.Contains(chars.GetValue(x).ToString()))
                        password += chars.GetValue(x);
                    else
                        i--;
                }

                return password;
            }
            catch (Exception)
            {
                return "";
            }
        
        }

        /// <summary>
        /// Generate alpha numeric password
        /// </summary>
        /// <param name="prmNoOfAlpha">Length of alpha</param>
        /// <param name="prmNoOfNumnber">Length of number</param>
        /// <returns></returns>
        public static string GenerateRandomAlphaNumericPassword(int prmNoOfAlpha, int prmNoOfNumnber)
        {
            try
            {
                //alpha
                var number_of_chars = prmNoOfAlpha;
                var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                var random = new Random();
                string result = new string(Enumerable.Repeat(chars, number_of_chars).Select(s => s[random.Next(s.Length)]).ToArray());

                //numeric
                var number_of_chars_Sec = prmNoOfNumnber;
                var charsSec = "0123456789";
                var randomSec = new Random();
                string resultSec = new string(Enumerable.Repeat(charsSec, number_of_chars_Sec).Select(s => s[randomSec.Next(s.Length)]).ToArray());

                return string.Format("{0}{1}", result, resultSec);
            }
            catch (Exception)
            {
                try
                {
                    Random randError = new Random();
                    string retErro = new string(Enumerable.Repeat(0, 9).Select(i => (char)randError.Next(65, 90)).ToArray());
                    return retErro;
                }
                catch (Exception)
                {

                    return "";
                }

            }

        }

        /// <summary>
        /// Generate random alpha password
        /// </summary>
        /// <returns></returns>
        public static string GenerateRandomAlphaPassword()
        {
            try
            {
                Random rand = new Random();
                string newPassword = new string(Enumerable.Repeat(0, 9).Select(i => (char)rand.Next(65, 90)).ToArray());
                return newPassword;
            }
            catch (Exception)
            {

                return "";
            }
        }

        /// <summary>
        /// Check for valid number
        /// </summary>
        /// <param name="ParamNumberString">Any text</param>
        /// <returns>true/false</returns>
        public static bool isNumeric(string ParamNumberString)
        {
            try
            {
                double storeNo;
                return double.TryParse(ParamNumberString, out storeNo);
            }
            catch (Exception)
            {

                return false;
            }
        }

        //public static List<string> GetEnumerationValueList(object ParamEnum)
        //{
        //    try
        //    {
        //        List<string> retCol = new List<string>();
        //        foreach (ParamEnum genre in Enum.GetValues(typeof(ParamEnum)))
        //        {
        //            retCol.Add(Enum.GetName(typeof(ParamEnum), genre));
        //        }

        //        return retCol;
        //    }
        //    catch (Exception)
        //    {

        //        return null;
        //    }
        //}

    }
}
